package com.heima.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserFollowService;
import com.heima.utils.thread.ApThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Slf4j
@Transactional
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    @Autowired
    private CacheService cacheService;

    /**
     * 关注/取消关注用户
     * 设计两张哈希表，一张为粉丝表，一张为关注表
     * 关注表结构
     *      redisKey            hashKey                     value
     *    userId_{userId}   follow_userId_{followUserId}      1
     * 粉丝表结构
     *      redisKey            hashKey                     value
     *    userId_{userId}   fans_userId_{fansUserId}          1
     * @param dto
     * @return
     */

    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        //1、检验参数
        if (null == dto.getOperation() ||
                null == dto.getArticleId() ||
                null == dto.getAuthorId()||
                dto.getOperation()>1||
                dto.getOperation()<0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUser apUser = ApThreadLocalUtil.getUser();
        /*//2、判断是关注还是取消关注
        if(dto.getOperation()==0){
            //关注
            ApUser author = apUserService.getById(dto.getAuthorId());
            if (null == author) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //2.1、填充apUserFollow信息，填写到数据库中
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(apUser.getId());
            apUserFollow.setCreateTime(new Date());
            apUserFollow.setIsNotice((short) 1);
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setFollowName(author.getName());
            apUserFollow.setLevel((short) 0);
            save(apUserFollow);
            //2.2填充apUserFans信息，填写到数据库中

        }else{
            //取消关注
            //删除数据库中的数据
            remove(Wrappers.<ApUserFollow>lambdaQuery().
                    eq(ApUserFollow::getUserId,apUser.getId()).
                    eq(ApUserFollow::getFollowId,dto.getAuthorId()));
        }*/
        //2、更新redis中的数据
        String followRedisKey = BehaviorConstants.Follow_Redis_key_prefix + apUser.getId();
        String followHashKey = BehaviorConstants.Follow_Hash_Key_prefix + dto.getAuthorId();
        String fansRedisKey = BehaviorConstants.Fans_Redis_Key_prefix + dto.getAuthorId();
        String fansHashKey = BehaviorConstants.Fans_Hash_Key_Prefix + apUser.getId();
        if(dto.getOperation()==0){
            //2.1新增粉丝/关注
            cacheService.hPut(followRedisKey,followHashKey,"1");
            cacheService.hPut(fansRedisKey,fansHashKey,"1");
        }else {
            //2.1删除
            cacheService.hDelete(followRedisKey,followHashKey);
            cacheService.hDelete(fansRedisKey,fansHashKey);
        }
        return ResponseResult.okResult(200,"操作成功");
    }

}
